package com.mvu.banana.tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Class description...
 *
 * @author gantz
 */
public class JavaField {
  protected String _modifier = "";
  protected JavaType type;
  protected String name;
  protected String _value;
  private JavaClass _parent;
  private List<JavaAnnotation> annotations = new ArrayList<JavaAnnotation>();
  protected Object[] info;

  public JavaField(JavaClass parent) {
    _parent = parent;
  }

  public JavaField withName(String name) {
    this.name = name;
    return this;
  }

  public JavaField withType(String type) {
    this.type = new JavaType(type);
    return this;
  }

  public JavaField withType(Class type) {
    _parent.importClass(type);
    this.type = new JavaType(type);
    return this;
  }

  public JavaField withType(JavaType fieldType) {
    type = fieldType;
    return this;
  }

  public JavaField withModifier(String modifier) {
    _modifier = modifier;
    return this;
  }

  public JavaField withValue(String value) {
    _value = value;
    return this;
  }

  public String print() {
    StringBuilder builder = new StringBuilder();
    if (annotations != null) {
      for (JavaAnnotation annotation : annotations) {
        builder.append("\n  ").append(annotation.print());
      }
    }
    builder.append("\n  ").append(_modifier).append(" ").append(type.name).append(" ")
            .append(name);
    if (_value != null) {
      builder.append(" = ").append(_value);
    }
    return builder.append(";").toString();
  }

  public JavaField withAnnotations(Class... annotations) {
    for(Class annotation : annotations){
      this.annotations.add(new JavaAnnotation(_parent, annotation));
    }
    return this;
  }

  public JavaAnnotation addAnnotation(Class annotationClass) {
    JavaAnnotation annotation = new JavaAnnotation(_parent, annotationClass);
    annotations.add(annotation);
    return annotation;
  }

  public void withMappingInfo(Object... info) {
    this.info = info;
  }

  public void addAnnotation(JavaAnnotation annotation) {
    annotations.add(annotation);
  }
}
